public class Main {
    public static void main(String[] args) {
        String searchStr = "dog";
        String mainStr = "This dog is a good dog.";
        int count = countOccurrences(mainStr, searchStr);
        System.out.println("The string \"" + searchStr + "\" appears " + count + " times in the string \"" + mainStr + "\".");
    }

    public static int countOccurrences(String mainStr, String searchStr) {
        int count = 0;
        int fromIndex = 0;
        while ((fromIndex = mainStr.indexOf(searchStr, fromIndex)) != -1) {
            count++; 
            fromIndex += searchStr.length(); 
        }

        return count;
    }
}
